#@head() 
#@header()
<title>营销管理</title>
#@aside("auxiliary")
<style>
	.select2-container{width:150px;float:left;height:35px;margin-top:10px;border-radius:0px !important;}
	.select2-container--default .select2-selection--single{height:35px;}
</style>
<div class="content-wrapper">
	<section class="content">
		<div class="nav-tabs-custom">
            <ul class="nav nav-tabs">
			  <li class="active"><a href="#(path)/admin/page/auxiliary-addcustom">添加员工</a></li>
            </ul>
            <div class="tab-content">
				  <!--推广参数配置--> 
			  <div class="tab-pane active" id="tab_2">
				<div class="box-body">
					<table id="example1" style="width:800px;" class="table table-striped">
						<tbody>
							<tr id="selectuser">
								<th align="right" style="width:100px;">选择用户：</th>
								<td>
									<div  id="a2">
										<select style="width:150px;height:35px;float:left;margin-top:10px;" id="userId" class="form-control"></select>
									</div> 
								</td>
									
							<tr>
								<th align="right" style="width:100px;">真实姓名：</th>
								<td>
									<input id="realName" disabled  type="text" class="form-control" placeholder="真实姓名">
									<input id="userdd" hidden type="text"  placeholder="用户id">
								</td>
							</tr>
							<tr>
								<th align="right" style="width:100px;">手机号：</th>
								<td>
									<input id="mobile"  type="text" class="form-control" placeholder="手机号">
								</td>
							</tr>
							<tr>
								<th align="right" style="width:100px;">工号：</th>
								<td>
									<input id="workcode"  type="text" class="form-control" placeholder="工号">
								</td>
							</tr>
							<tr>
								<th align="right"  style="width:100px;">角色：</th>
								<td>
									 <select id="role" class="form-control">
										<option value="admin">管理员</option>
										<option value="agent">普通客服</option>
										<option value="agent_leader">客服组长</option>
										<option value="agent_manager">客服主管</option>
										<option value="assets_create">资产发布</option>
										<option value="assets_first">资产一审</option>
										<option value="assets_second">资产终审</option>
										<option value="product">营销管理</option>
										<option value="active">活动管理</option>
										<option value="plat">融资机构</option>
									 </select>
								</td>
							</tr>
							<tr id="tr1" hidden>
								<th align="right" style="width:100px;">当月指标：</th>
								<td>
									<input id="target" value="0" type="text" class="form-control" placeholder="当月指标（万元）">
								</td>
							</tr>
							<tr id="tr2" hidden>
								<th align="right"  style="width:100px;">客服分组</th>
								<td>
									 <select id="groups" class="form-control">
										<option value="" >请选择分组</option>
										<option value="A" >A组</option> 
										<option value="B" >B组</option>
										<option value="C" >C组</option>
									 </select>
								</td>
							</tr>
							<tr id="selectuser">
								<th align="right" style="width:100px;">所属上级：</th>
								<td>
									<select style="width:150px;height:35px;float:left;margin-top:-10px;" id="userId5" class="form-control"></select>
									<span id="leaderName" style="margin-left:20px;line-height:50px;"></span>
								</td>
							<tr>
						</tbody>
					</table>
				</div>
				<div class="box-footer">
					<button id="submit" type="submit" style="margin-left:10px;" class="btn btn-info ">提交</button>
					<button onclick="window.location.href='#(path)/admin/page/auxiliary-customlist'" style="margin-left:10px;" class="btn btn-default">取消</button>
				  </div>
              </div>
            </div>
            <!-- /.tab-content -->
          </div>
		  
	</section>
</div>
#@footer()
<script>
$(document).ready(function(){
	var id = GetRequest(id).id;
	if(!$.isBlank(id)){
		$("#selectuser").hide();
		$.getJSON("/staff/info?token=#(session.token??)&id="+id, function(agentinfo) {
			console.log(agentinfo);
			$("#realName").val(agentinfo.data.real_name);
			$("#userdd").val(agentinfo.data.user_id);
			$("#mobile").val(agentinfo.data.mobile);
			$("#workcode").val(agentinfo.data.workcode);
			$("#role").val(agentinfo.data.role);
			$("#groups").val(agentinfo.data.groups);
			$("#target").val(agentinfo.data.target);
			$("#leaderName").html(agentinfo.data.leaderName);
			if(agentinfo.data.role=="agent"||agentinfo.data.role=="agent_leader"){
			$("#tr1").show()
			$("#tr2").show()
			}
		});
	}
	if($.isBlank(id)){
		id="";
	}
 
	$("#userId5").select2({
		ajax: {
			type:'GET',
			url: "#(path)/user/list?token=#(session.token??)",
			dataType: 'json',
			delay: 1000,
			data: function (params) {
				return {
					mobile: params.term, // search term 请求参数
					page: params.page
				};
			},
			processResults: function (data, params) {
				var itemList = [];
				var arr = data.data.page.list
				for(item in arr){
					var user = arr[item];
					itemList.push({id:user.id, text:user.real_name});
				}
				return {
					results: itemList
				 };
			},
			cache: true
		},
		placeholder:'请输入号码搜索...',//默认文字提示
		language: "zh-CN",
		tags: false,//允许手动添加
		allowClear: true,//允许清空
		escapeMarkup: function (markup) { return markup; }, // 自定义格式化防止xss注入
		minimumInputLength: 1,//最少输入多少个字符后开始查询
		formatResult: function formatRepo(repo){return repo.text;}, // 函数用来渲染结果
		formatSelection: function formatRepoSelection(repo){return repo.text;} // 函数用于呈现当前的选择
	});
	$("#userId").select2({
		ajax: {
			type:'GET',
			url: "#(path)/user/list?token=#(session.token??)",
			dataType: 'json',
			delay: 1000,
			data: function (params) {
				return {
					mobile: params.term, // search term 请求参数
					page: params.page
				};
			},
			processResults: function (data, params) {
				var itemList = [];
				var arr = data.data.page.list
				for(item in arr){
					var user = arr[item];
					$("#realName").val(user.real_name);
					$("#mobile").val(user.mobile);
					$("#userdd").val(user.id);
					itemList.push({id:user.id, text:user.real_name});
				}
				return {
					results: itemList
				 };
			},
			cache: true
		},
		placeholder:'请输入号码搜索...',//默认文字提示
		language: "zh-CN",
		tags: false,//允许手动添加
		allowClear: true,//允许清空
		escapeMarkup: function (markup) { return markup; }, // 自定义格式化防止xss注入
		minimumInputLength: 1,//最少输入多少个字符后开始查询
		formatResult: function formatRepo(repo){return repo.text;}, // 函数用来渲染结果
		formatSelection: function formatRepoSelection(repo){return repo.text;} // 函数用于呈现当前的选择
	});
	$("#role").change(function(){
		if($("#role").val()=="agent_leader"||$("#role").val()=="agent"){
			$("#tr1").show();
			$("#tr2").show();
			$("#groups").val()="A";
			
		}else{
			$("#tr1").hide();
			$("#tr2").hide();
		}
	}); 
	$("#submit").click(function() {
		var userdd = $("#userdd").val();
		var realName = $("#realName").val();
		var mobile = $("#mobile").val();
		var workcode = $("#workcode").val();
		var role = $("#role").val();
		var target = $("#target").val();
		var groups = $("#groups").val();
		if($("#userId5").val()==null){
			var leader="";
		}else{
			var leader = $("#userId5").val();
		}
		layer.confirm('是否确认提交？', {btn : [ '确认', '取消' ]}, function() {
			ajax("#(path)/staff/edit?token=#(session.token??)&leader="+leader+"&groups="+groups+"&userId="+userdd+"&realName="+realName+"&target="+target+"&mobile="+mobile+"&id="+id+"&workcode="+workcode+"&role="+role,{},function(data){
				if(data.code=='success'){
					layer.msg(data.msg, {icon:1},function(){
						location.href="#(path)/admin/page/auxiliary-customlist"
					});
				}else if(data.code=='error'){
					layer.msg(data.msg, {icon : 2});
				}
			});
		}, function() {
			layer.msg('已取消！', {icon : 1});
		});
	});
	function GetRequest() {  
	   var url = location.search; //获取url中"?"符后的字串  
	   var theRequest = new Object();  
	   if (url.indexOf("?") != -1) {  
		  var str = url.substr(1);  
		  strs = str.split("&");  
		  for(var i = 0; i < strs.length; i ++) {  
			 theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);  
		  }  
	   }  
	   return theRequest;  
	}  
});
</script>
</body>
</html>

